package day20170416.io.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class BufferedInOutStream {
	
	/**
	 * 缓存流
	 * @throws Exception
	 */
	@Test
	public void bufferedInOutStreamTest() throws Exception {
		bufferedInOutStream("C:/Users/Administrator/Desktop/新建文件夹/深圳布吉联通.txt", "C:/Users/Administrator/Desktop/深圳布吉联通.txt");
	}
	public void bufferedInOutStream(String src,String dest){
		//定义2个缓存流进行加速
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		//选择流
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			//文件路径
			File file = new File(src);
			File file1 = new File(dest);
			//把文件路径装进管道
			fis = new FileInputStream(file);
			fos = new FileOutputStream(file1);
			//使用缓存流进行加速
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			byte[] bs = new byte[1];
			int len;
			int i = 0;
			while((len = bis.read(bs)) != -1){
				if(i == 12){
					i = 0;
					bs[bs.length-1] = ',';
					bos.write(bs, 0, len);
					bos.flush();
				}else{
					i++;
					bos.write(bs, 0, len);
					bos.flush();
				}
			}
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bos != null){
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
